﻿using System;
using System.Reflection;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using ipi.MCC.Global;

namespace ipi.MCC.Controls
{
    public delegate void OnNavigationSelectedHandler(int iNodeID);

	public class MCCTreePath : System.Web.UI.UserControl
	{
        protected Repeater rptNavigationBar;
        protected CustomValidator validGlobal;
        public event OnNavigationSelectedHandler OnNavigationSelected;

        public void SetTreePath(TreeNode NodePath)
        {
            try
            {
                DataTable dtTreePath = new DataTable();
                DataColumn dcLevelRec = new DataColumn();
                dcLevelRec.ColumnName = "LevelRec";
                dcLevelRec.DataType = System.Type.GetType("System.Int32");
                dcLevelRec.AllowDBNull = false;
                dtTreePath.Columns.Add(dcLevelRec);

                DataColumn dcName = new DataColumn();
                dcName.ColumnName = "NodeName";
                dcName.DataType = System.Type.GetType("System.String");
                dcName.AllowDBNull = false;
                dtTreePath.Columns.Add(dcName);

                DataColumn dcNodeID = new DataColumn();
                dcNodeID.ColumnName = "NodeID";
                dcNodeID.DataType = System.Type.GetType("System.Int32");
                dcNodeID.AllowDBNull = true;
                dtTreePath.Columns.Add(dcNodeID);

                int iLevelRec = 1;
                TreeNode tnParentNode = NodePath;
                do
                {
                    DataRow drCurrent = dtTreePath.NewRow();

                    drCurrent[dcLevelRec] = iLevelRec++;
                    drCurrent[dcName] = tnParentNode.Text;

                    if (tnParentNode == NodePath)
                    {
                        drCurrent[dcNodeID] = DBNull.Value;
                    }
                    else
                    {
                        drCurrent[dcNodeID] = tnParentNode.Value;
                    }
                    dtTreePath.Rows.Add(drCurrent);

                    tnParentNode = tnParentNode.Parent;
                }
                while (tnParentNode != null);

                rptNavigationBar.DataSource = new DataView(dtTreePath, "", "LevelRec desc", DataViewRowState.CurrentRows);
                rptNavigationBar.DataBind();
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
            }
        }
        
        protected void rptNavigationBar_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (OnNavigationSelected != null)
            {
                switch (e.CommandName)
                {
                    case "ChangePosition":
                        OnNavigationSelected(Convert.ToInt32(e.CommandArgument));
                        break;

                    default:
                        break;
                }
            }
        }
        protected void rptNavigationBar_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //DataRowView drv =(DataRowView)e.Item.DataItem;
                DataRowView drv = (DataRowView)e.Item.DataItem;
                LinkButton lnkNavi = (LinkButton)e.Item.FindControl("lnkTreePosition");
                Label lblNavi = (Label)e.Item.FindControl("lblTreePosition");

                if (drv["NodeID"] == DBNull.Value)
                {
                    lnkNavi.Visible = false;
                    lblNavi.Visible = true;
                }
                else
                {
                    lnkNavi.Visible = true;
                    lblNavi.Visible = false;
                }
            }
        }
	}
}
